package com.online.fund.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	/**
	 * 
	 * 方法用途: 获取当前日期<br>
	 * 实现步骤: <br>
	 * @return
	 */
	public static String getCurrentDate() {
		LocalDate today = LocalDate.now();
		return today.toString();
	}
	
	/**
	 * 获取当前日期时间
	 * @return
	 */
	public static String getCurrentDatetime() {
		LocalDateTime datetime = LocalDateTime.now();
		DateTimeFormatter format = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
		return format.format(datetime);
	}

	/**
	 * 
	 * 方法用途: 日期天数的加减<br>
	 * 实现步骤: <br>
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date addDate(Date date, int days) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.DAY_OF_MONTH, days);
		return calendar.getTime();
	}
	
	/**
	 * 
	 * @param startTime
	 * @param endTime
	 * @param format
	 * @param str(h：小时，m：分，s：秒)
	 * @return
	 */
	public static Long dateDiff(String startTime, String endTime,  String format, String resultType) {   
        // 按照传入的格式生成一个simpledateformate对象    
        DateFormat sd = new SimpleDateFormat(format);    
        long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数    
        long nh = 1000 * 60 * 60;// 一小时的毫秒数    
        long nm = 1000 * 60;// 一分钟的毫秒数    
        long ns = 1000;// 一秒钟的毫秒数    
        long diff;    
        long day = 0;    
        long hour = 0;    
        long min = 0;    
        long sec = 0;    
        // 获得两个时间的毫秒时间差异    
        try {    
            diff = sd.parse(endTime).getTime() - sd.parse(startTime).getTime();    
            day = diff / nd;// 计算差多少天    
            hour = diff % nd / nh + day * 24;// 计算差多少小时    
            min = diff % nd % nh / nm + day * 24 * 60;// 计算差多少分钟    
            sec = diff % nd % nh % nm / ns;// 计算差多少秒    
            // 输出结果    
            System.out.println("时间相差：" + day + "天" + (hour - day * 24) + "小时"   
                    + (min - day * 24 * 60) + "分钟" + sec + "秒。");    
            System.out.println("hour=" + hour + ",min=" + min);    
            if (resultType.equalsIgnoreCase("h")) {    
                return hour;    
            } else if (resultType.equalsIgnoreCase("m")) {    
                return min;    
            } else{    
                return sec;    
            }    
   
        } catch (ParseException e) {    
            e.printStackTrace();    
        }    
        if (resultType.equalsIgnoreCase("h")) {    
            return hour;    
        } else if (resultType.equalsIgnoreCase("m")) {    
            return min;    
        } else{    
            return min;    
        }    
}  
	
	public static void main(String[] args) {
		System.out.println(getCurrentDatetime());
//		System.out.println(dateDiff("2018-02-20 22:32:01", "2018-02-20 22:32:08", "yyyy-MM-dd HH:mm:ss", "s"));
	}
}
